package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Teacher;
import com.production.CoursePlatform.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    // 根据 tID 查询 Teacher
    @GetMapping("/findByTID/{tID}")
    public ResponseEntity<Teacher> findByTID(@PathVariable String tID) {
        Teacher teacher = teacherService.findTeacherByTID(tID);
        if (teacher != null) {
            return ResponseEntity.ok(teacher);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 添加 Teacher
    @PostMapping("/add")
    public ResponseEntity<String> addTeacher(@RequestBody Teacher teacher) {
        int result = teacherService.addTeacher(teacher);
        if (result > 0) {
            return ResponseEntity.status(HttpStatus.CREATED).body("Teacher added successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add teacher");
        }
    }

    // 更新 Teacher
    @PutMapping("/update")
    public ResponseEntity<String> updateTeacher(@RequestBody Teacher teacher) {
        int result = teacherService.updateTeacher(teacher);
        if (result > 0) {
            return ResponseEntity.ok("Teacher updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Failed to update teacher");
        }
    }

    // 根据 tID 删除 Teacher
    @DeleteMapping("/delete/{tID}")
    public ResponseEntity<String> deleteTeacher(@PathVariable String tID) {
        int result = teacherService.deleteTeacher(tID);
        if (result > 0) {
            return ResponseEntity.ok("Teacher deleted successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Failed to delete teacher");
        }
    }

    // 查询所有 Teacher
    @GetMapping("/findAll")
    public ResponseEntity<List<Teacher>> findAllTeachers() {
        List<Teacher> teachers = teacherService.findAllTeachers();
        return ResponseEntity.ok(teachers);
    }
}

